﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Admission
{
    public class StudentTransaction
    {
        private static SchoolEntities db = new SchoolEntities();


        public static long Add(ADM_Student_Transaction studenttransaction)
        {
            try
            {
                db.AddToADM_Student_Transaction(studenttransaction);
                db.SaveChanges();
                return studenttransaction.Id;

            }

            catch
            {
                return 0;

            }
        }

        public static ADM_Student_Transaction Single(long id)
        {
            return db.ADM_Student_Transaction.Single(x => x.Id == id);
        }
     
        public static ORG_EductionalYear  GetCurrentEducationalYear()
        {
            return db.ORG_EductionalYear.Single(x => x.IsCurrent == true );
        }


       public static IEnumerable<ADM_Student_Transaction> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from studenttransaction in db.ADM_Student_Transaction select studenttransaction;

            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Student_Id")
                {
                    query = query.Where(c => c.Student_Id == (long)item.Value);
                }

                if (item.Key == "Status_Id")
                {
                    query = query.Where(c => c.Status_Id == (long)item.Value);
                }


                if (item.Key == "EductionalYear_Id")
                {
                    query = query.Where(c => c.EductionalYear_Id == (long)item.Value);
                }



                if (item.Key == "EductionalYear_Id")
                {
                    query = query.Where(c => c.EductionalYear_Id == (long)item.Value);
                }



            }
            return query.ToList();
        }

        public static void Update(ADM_Student_Transaction studenttransaction)
        {
            ADM_Student_Transaction c = db.ADM_Student_Transaction.Single(x => x.Id == studenttransaction.Id);


            c.Id = studenttransaction.Id;
            c.Student_Id = studenttransaction.Student_Id;

            c.Status_Id = studenttransaction.Status_Id;
            c.EductionalYear_Id = studenttransaction.EductionalYear_Id;
            c.Campus_Id = studenttransaction.Campus_Id;
            c.Certificate_Id = studenttransaction.Certificate_Id;
            c.Stage_Id = studenttransaction.Stage_Id;
            c.Grad_Id = studenttransaction.Grad_Id;


            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.ADM_Student_Transaction.DeleteObject(db.ADM_Student_Transaction.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
